<?php

class ome_mdl_dly_corp extends dbeav_model{

    function  save(&$data,$mustUpdate = null){
        $Odly_corp_area = &$this->app->model('dly_corp_area');
        
        if($data['protect']) $data['protect_rate'] = $data['protect_rate']/100;
        $data['ordernum'] = intval($data['ordernum']);
        if($data['area_fee_conf'] && is_array($data['area_fee_conf'])){
            foreach($data['area_fee_conf'] as $key=>$value){
                if($value['dt_useexp']==0){//如果未使用公式则使用默认
                    $data['area_fee_conf'][$key]['dt_expressions'] = "{{w-0}-0.4}*{{{".$data['firstunit']."-w}-0.4}+1}*".$value['firstprice']."+ {{w-".$data['firstunit']."}-0.6}*[(w-".$data['firstunit'].")/".$data['continueunit']."]*".$value['continueprice']."";
                }
                if($data['corp_id']!=''){
                    $Odly_corp_area->get_corp_area($data['corp_id'],$value['areaGroupId']);
                }
            }
            $data['area_fee_conf'] = serialize($data['area_fee_conf']);
        }else{
            if($data['dt_useexp']==0){//如果未使用公式则使用默认
                $data['dt_expressions'] = "{{w-0}-0.4}*{{{".$data['firstunit']."-w}-0.4}+1}*".$data['firstprice']."+ {{w-".$data['firstunit']."}-0.6}*[(w-".$data['firstunit'].")/".$data['continueunit']."]*".$data['continueprice']."";
            }
        }
    
      return parent::save($data,$mustUpdate);
    }

    //获取物流公司指定的配送地区
    function get_corp_region(){
        $corpAreaObj = &$this->app->model('dly_corp_area');
        $rows = $corpAreaObj->getList('corp_id,region_id');
        $corp_region = array();
        foreach($rows as $v){
            $corp_region[$v['corp_id']][] = $v['region_id'];
        }
        return $corp_region;
    }
    
    function getRegionById($parent_id){
        $sql='select r.region_id,r.p_region_id,r.local_name,count(p.region_id) as child_count from sdb_ectools_regions as r
                left join sdb_ectools_regions as p on r.region_id=p.p_region_id
                where r.p_region_id'.($parent_id?('='.intval($parent_id)):' is null').' and r.package=\''.$this->app->getConf('system.location').'\'
                group by(r.region_id)
                order by r.ordernum asc,r.region_id';

        return $this->db->select($sql);
    }
    
    function searchOptions(){
        return array(
                
            );
    }
    
    function corp_default(){
        $dly_corp = array(
         'EMS' => array('name' => 'EMS','type' => 'EMS','website' => 'http://www.ems.com.cn/','request_url' => 'http://www.ems.com.cn/',),
		'STO' => array('name' => '申通E物流','type' => 'STO','website' => 'http://www.sto-express.com.cn/','request_url' => 'hhttp://www.sto-express.com.cn/',),
		'YTO' => array('name' => '圆通速递','type' => 'YTO','website' => 'http://www.yto.net.cn/','request_url' => 'http://www.yto.net.cn/',),
		'ZTO' => array('name' => '中通速递','type' => 'ZTO','website' => 'http://www.zto.cn/','request_url' => 'http://www.zto.cn/',),
		'YUNDA' => array('name' => '韵达快运','type' => 'YUNDA','website' => 'http://www.yundaex.com/www/index.html','request_url' => 'http://www.yundaex.com/www/index.html',),
		'TTKDEX' => array('name' => '天天快递','type' => 'TTKDEX','website' => 'http://www.ttkd.cn/','request_url' => 'http://www.ttkd.cn/',),
		'FEDEX' => array('name' => '联邦快递','type' => 'FEDEX','website' => 'http://www.fedex.com/cn/','request_url' => 'http://www.fedex.com/cn/',),
		'HTKY' => array('name' => '汇通快运','type' => 'HTKY','website' => 'http://www.htky365.com/','request_url' => 'http://www.htky365.com/',),
		'DBL' => array('name' => '德邦物流','type' => 'DBL','website' => 'http://www.deppon.com/','request_url' => 'http://www.deppon.com/',),
		'SF' => array('name' => '顺丰速运','type' => 'SF','website' => 'http://www.sf-express.com/tabid/41/Default.aspx','request_url' => 'http://www.sf-express.com/tabid/41/Default.aspx',),
		'POST' => array('name' => '邮政平邮','type' => 'POST','website' => 'http://yjcx.chinapost.com.cn/','request_url' => 'http://yjcx.chinapost.com.cn/',),
		'CRE' => array('name' => '中铁快运','type' => 'CRE','website' => 'http://www.95572.com/jsp/index.jsp','request_url' => 'http://www.95572.com/jsp/index.jsp',),
		'CCES' => array('name' => 'CCES','type' => 'CCES','website' => 'http://www.cces.com.cn/','request_url' => 'http://www.cces.com.cn/',),
		'AIRFEX' => array('name' => '亚风','type' => 'AIRFEX','website' => 'http://www.broad-asia.net/','request_url' => 'http://www.broad-asia.net/',),
		'APEX' => array('name' => '全一','type' => 'APEX','website' => 'http://www.apex100.com/','request_url' => 'http://www.apex100.com/',),
		'LBEX' => array('name' => '龙邦','type' => 'LBEX','website' => 'http://www.lbex.com.cn/','request_url' => 'http://www.lbex.com.cn/',),
		'CYEXP' => array('name' => '长宇','type' => 'CYEXP','website' => '','request_url' => '',),
		'DTW' => array('name' => '大田','type' => 'DTW','website' => 'http://www.dtw.com.cn/','request_url' => 'http://www.dtw.com.cn/',),
		'YUD' => array('name' => '长发','type' => 'YUD','website' => '','request_url' => '',),
		'ANTO' => array('name' => '安得','type' => 'ANTO','website' => '','request_url' => '',),
		'EBON' => array('name' => '一邦速递','type' => 'EBON','website' => 'http://www.ebon-express.com/','request_url' => 'http://www.ebon-express.com/',),
		'STARS' => array('name' => '星晨急便','type' => 'STARS','website' => 'http://www.4006688400.com/','request_url' => 'http://www.4006688400.com/',),
		'ZJS' => array('name' => '宅急送','type' => 'ZJS','website' => 'http://www.zjs.com.cn/','request_url' => 'http://www.zjs.com.cn/',),
		'CNEX' => array('name' => '佳吉快运','type' => 'CNEX','website' => 'http://www.jiaji.com/','request_url' => 'http://www.jiaji.com/',),
		'e邮宝' => array('name' => 'e邮宝','type' => 'e邮宝','website' => 'http://www.eyoubao.cn/','request_url' => 'http://www.eyoubao.cn/',),
		'SHQ' => array('name' => '华强物流','type' => 'SHQ','website' => '','request_url' => '',),
		'XB' => array('name' => '新邦物流','type' => 'XB','website' => 'http://www.xbwl.cn/website/main.jsp','request_url' => 'http://www.xbwl.cn/website/main.jsp',),
		'UC' => array('name' => '优速物流','type' => 'UC','website' => 'http://www.uc56.com/','request_url' => 'http://www.uc56.com/',),
		'FAST' => array('name' => '快捷速递','type' => 'FAST','website' => 'http://www.fastexpress.com.cn/news.do?method=listsy','request_url' => 'http://www.fastexpress.com.cn/news.do?method=listsy',),
		'XFHONG' => array('name' => '鑫飞鸿快递','type' => 'XFHONG','website' => 'http://www.xfhex.cn/','request_url' => 'http://www.xfhex.cn/',),
		'NEDA' => array('name' => '港中能达','type' => 'NEDA','website' => 'http://www.nd56.com/','request_url' => 'http://www.nd56.com/',),
		'QRT' => array('name' => '全日通快递','type' => 'QRT','website' => 'http://www.at-express.com/','request_url' => 'http://www.at-express.com/',),
		'SAD' => array('name' => '赛澳递','type' => 'SAD','website' => 'http://www.51cod.com/','request_url' => 'http://www.51cod.com/',),
        
        );
        
        return $dly_corp;
    }

    function set_area($region_ids,$dly_corp_id){
        $areaObj = &$this->app->model('dly_corp_area');
        $region_ids = kernel::single('ome_region')->get_region_node($region_ids);
        foreach ($region_ids as $area_id){
            $data['corp_id'] = $dly_corp_id;
            $data['region_id'] = $area_id;
            $areaObj->save($data);
        }
    }
}

?>
